// 下拉框  value => label
export const selectToLabel = (arr: any, val: any, label?: string, value?: string) => {
  if (label) {
    let list = arr.filter((item: any) => item[value ?? ''] == val)
    if (!list.length) return ''
    return list[0][label]
  } else {
    let list = arr.filter((item: any) => item.value == val)
    if (!list.length) return ''
    return list[0].label
  }
}

// 数值颜色
export const colorClass = (num: any) => {
  let color = 'colorGre'
  if (Number(num) <= 0) {
    color = 'colorRed'
  }
  return color
}

// 查询当前域名是哪个运营商
export const getOperatorInfo = (value: string) => {
  if(value.includes('gmapi.cc')){
    return 'gmapi.cc'
  }else{
    return 'omgapi.cc'
  }
}

// 数组每x个元素 分一组
export function arrayGroup(arr: any, chunkSize: number) {
  return arr.reduce((chunks: any, item: any, i: any) => {
    const chunkIndex = Math.floor(i / chunkSize)
    if (!chunks[chunkIndex]) {
      chunks[chunkIndex] = []
    }
    chunks[chunkIndex].push(item)
    return chunks
  }, [])
}

// 按照数组对象的某个值排序
export function sortsObjectArray(arr: any, key: string, type: string){
  return arr.sort(function (a: any, b: any) {
    var x = a[key]
    var y = b[key]
    if(type=='desc'){
      return x > y ? -1 : x < y ? 1 : 0
    }else{
      return x < y ? -1 : x > y ? 1 : 0
    }
  })
}
// 按照数组排序
export function sortsArray(arr: any,type:string){
  return arr.sort(function (a: any, b: any) {
    if(type=='desc'){
      return Number(a) > Number(b) ? -1 : Number(a) < Number(b) ? 1 : 0
    }else{
      return Number(a) < Number(b) ? -1 : Number(a) > Number(b) ? 1 : 0
    }
  })
}

// 防抖
export function throttle(fn: Function, wait: number) {
  let timer: any
  return function (query?: any) {
    if (timer != null) return
    // @ts-ignore
    const context = this
    const args = arguments
    fn.apply(context, args)
    timer = setTimeout(() => {
      timer = null
    }, wait)
  }
}